local run = true
local xSize, ySize = term.getSize()
local xCenter, yCenter = math.floor(xSize/2), math.floor(ySize/2-4)
local screen = 1
local maxScreen = 2
local intro = "OS beta installer"

local clear = function(color)
 term.setCursorPos(1,1)
 term.setBackgroundColor(color)
 term.clear()
end

local usualText = function(string,xPos,yPos)
 term.setCursorPos(xPos,yPos)
 term.write(string)
end

local function restore(chto,kuda)
	local file = fs.open(chto,"r")
	Massiv = textutils.unserialise(file.readAll())
	file.close()
	for i=1,#Massiv do
		if Massiv[i]["text"] == "@empty@empty" then
			fs.makeDir(kuda.."/"..Massiv[i]["path"])
		else
			local file = fs.open(kuda.."/"..Massiv[i]["path"],"w")
			if Massiv[i]['text'] ~= nil then
				file.write(Massiv[i]["text"])
			else
				file.write(' ')
			end
			file.close()
		end
	end
	fs.delete('/System/desks/desk.back')
	fs.delete('/System/accounts/accs.back')
end

local pastebin = function(paste,filename)
 local file = http.get("http://pastebin.com/raw.php?i="..paste)
 if file then
  file = file.readAll()
  h=fs.open(filename,"w")
  h.write(file)
  h.close()
 else
  clear(colors.black)
  term.setTextColor(colors.white)
  usualText("ERROR!",xSize/2-6,ySize/2)
 end
end

local redraw = function()
 clear(colors.black)
 local bar = function()
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.lightGray)
  usualText(intro,1,1)
 end
 if screen == 1 then
  clear(colors.white)
  bar()
  term.setTextColor(colors.black)
  term.setBackgroundColor(colors.lime)
  term.setTextColor(colors.white)
  usualText(" Next ",xSize-6,ySize-1)
  term.setBackgroundColor(colors.gray)
  usualText(" Exit ",2,ySize-1)
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  usualText("This program will install",xCenter-11,yCenter)
  usualText("beta version of VedroidOS",xCenter-11,yCenter+1)
  term.setTextColor(colors.red)
  usualText("WARNING:",xCenter-11,yCenter+3)
  usualText("It may be unstable",xCenter-11,yCenter+4)
  usualText("Your Files may be deleted",xCenter-11,yCenter+5)
  term.setTextColor(colors.lightGray)
  usualText("If you want to continue,",xCenter-11,yCenter+7)
  usualText('you should press "Next"',xCenter-11,yCenter+8)
 elseif screen == 2 then
  clear(colors.white)
  bar()
  term.setTextColor(colors.lightGray)
   usualText("Updating:",xCenter-11,ySize-4)
  term.setTextColor(colors.black)
  paintutils.drawLine(xCenter-11,ySize-1,xCenter+12,ySize-1,colors.gray)
  paintutils.drawPixel(xCenter-11,ySize-1,colors.lime)
   term.setBackgroundColor(colors.white)
   usualText("Downloading rom...",xCenter-11,ySize-3)
   pastebin("Ld2SfQNW","/OSrom")
   sleep(1)
  paintutils.drawLine(xCenter-11,ySize-1,xCenter+1,ySize-1,colors.lime)
   term.setBackgroundColor(colors.white)
   usualText("Flashing rom...   ",xCenter-11,ySize-3)
   restore('/OSrom','')
   fs.delete('/OSrom')
   sleep(1)
  paintutils.drawLine(xCenter-11,ySize-1,xCenter+12,ySize-1,colors.lime)
  sleep(0.5)
  clear(colors.white)
  term.setTextColor(colors.lime)
  usualText("Done",xCenter-2,yCenter+4)
  sleep(0.5)
  run = false
 end
end

local start = function()
 while run do
  event, side, x, y = os.pullEvent()
  if event == "mouse_click" then
   if x>=xSize-6 and x<=xSize-1 and y==ySize-1 then
    screen = 2
    redraw()
   elseif x>=1 and x<=7 and y==ySize-1 then
    run = false
   end
  end
 end
end

redraw()
start()
clear(colors.black)
fs.delete("/System/prog/up")
os.reboot()